/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

/**
 * 
 */
package edu.ucla.cs.typecast.discovery;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;

import edu.ucla.cs.typecast.event.EventManager;
import edu.ucla.cs.typecast.event.EventSource;
import edu.ucla.cs.typecast.net.EndPoint;
import edu.ucla.cs.typecast.net.NetUtil;
import edu.ucla.cs.typecast.net.TypeCastConstants;
import edu.ucla.cs.typecast.rmi.Fact;
import edu.ucla.cs.typecast.rmi.Future;
import edu.ucla.cs.typecast.rmi.TypeCastClient;
import edu.ucla.cs.typecast.rmi.server.ServiceRegistrationEntry;
import edu.ucla.cs.typecast.rmi.server.TypeCastMetaService;
import edu.ucla.cs.typecast.rmi.server.TypeCastObjectManager;

/**
 *
 * @date Aug 8, 2007
 */
public class ServiceDiscoveryServer implements TypeCastMetaService, ServiceDiscoveryService, TypeCastConstants{
	public void discover(Class[] types, ServiceTemplate template) {
		try { 
			discover(types, EndPoint.DEFAULT_RMI_ENDPOINT, template); 
		} catch(Throwable t) { 
			t.printStackTrace(); 
		}
		
	}
	
	public void discover( Class[] types, EndPoint endPoint, ServiceTemplate template) throws IOException { 
		try { 
			Collection<ServiceRegistrationEntry> entries = TypeCastObjectManager.getInstance().getServiceRegistrationEntries(template.getTypes()); 
			if (entries.size() > 0) { 
				//EventSource discoveryChannel = EventManager.getInstance().getEventSource(InetAddress.getByName(DEFAULT_DISCOVERY__EVENT_ADDRESS), DEFAULT_DISCOVERY_EVENT_PORT, template.getTypes());
				for(ServiceRegistrationEntry entry : entries) { 
					ServiceAdvertisement sa = new ServiceAdvertisement(NetUtil.getLocalHost(),  entry.getTypes(), new EndPoint[] { entry.getEndPoint() }, entry.getGUID(), template.getSeqno()); 									
					TypeCastClient client = new TypeCastClient(endPoint); 
					ServiceAdvertisementListener discoverListeners = client.getInvocationHandler(ServiceAdvertisementListener.class); 
					discoverListeners.notify(sa); 
				}
			}
		} catch(Throwable t) { 
			t.printStackTrace(); 
		}
	}

	public Future<ServiceAdvertisement[]>  discoverWithUnicastReturn(Class[] types, ServiceTemplate template) { 
		try { 
			Collection<ServiceRegistrationEntry> entries = TypeCastObjectManager.getInstance().getServiceRegistrationEntries(template.getTypes()); 
			if (entries.size() > 0) { 
				ArrayList<ServiceAdvertisement> result = new ArrayList<ServiceAdvertisement>(); 
				for(ServiceRegistrationEntry entry : entries) { 
					ServiceAdvertisement sa = new ServiceAdvertisement(NetUtil.getLocalHost(),  entry.getTypes(), new EndPoint[] { entry.getEndPoint() }, entry.getGUID()); 									
					result.add(sa); 
				}
				ServiceAdvertisement[] sas = new ServiceAdvertisement[result.size()]; 
				result.toArray(sas); 
				return new Fact(sas); 
			}
			return null; 
			
		} catch(Throwable t) { 
			t.printStackTrace(); 
			return null; 
		}
		
	}
}
